/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cs.util.dom;

import com.cs.util.dom.transform.DocumentTransformer;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.transform.TransformerException;

/**
 * DocumentConverter can be used for converting a String to Document and vice versa.
 * @author dimitris@jmike.gr
 */
public class DocumentConverter {

    /** Parse string as an XML document and return org.w3c.dom.Document object */
    public static Document parse(String xml) throws ParserConfigurationException, IOException, SAXException {
        if (xml != null && !xml.equals("")) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(new InputSource(new StringReader(xml)));
        } else {
            return null;
        }
    }

    /** Parse string as an XML document, validate it with schema (XSD) and return org.w3c.dom.Document object */
    public static Document parse(String xml, String schemaLocation) throws ParserConfigurationException, SAXException, SAXParseException, IOException {
        if (xml != null && !xml.equals("")) {
            /* Create a DocumentBuilderFactory */
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);//Configure the factory to produce parsers which are namespace aware
            factory.setValidating(true);//Configure the factory to produce parsers which validate the XML content during parse
            /* Set schema language as XML Schema (XSD) */
            factory.setAttribute(
                    "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");
            /* Load XSD to be used for validation */
            factory.setAttribute(
                    "http://java.sun.com/xml/jaxp/properties/schemaSource",
                    schemaLocation);
            /* Create a new DocumentBuilder */
            DocumentBuilder builder = factory.newDocumentBuilder();
            /* Create and register an ErrorHandler with the builder */
            SAXErrorHandler h = new SAXErrorHandler();
            builder.setErrorHandler(h);
            /* Parse XML */
            Document doc = builder.parse(new InputSource(new StringReader(xml)));
            //doc = parser.parse("http://localhost:8080/cloudedsunday.com/test.xml");//For testing purposes
            if (h.validationError != true) {
                return doc;
            } else {
                throw h.saxParseException;
            }
        } else {
            throw new SAXException();
        }
    }

    /** Parse XML document as String */
    public static String parse(Document xml) throws TransformerConfigurationException, TransformerException {
        if (xml != null) {
            DocumentTransformer xslt = new DocumentTransformer();
            return xslt.getString(xml);
        } else {
            return null;
        }
    }
}
